//
//  DCOrderReceiveController.m
//  JiuyaoMotorcade
//
//  Created by 大橙子 on 2018/11/28.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCOrderReceiveController.h"
#import "DCReceiveOrderHeader.h"
#import "DCReceiveOrderCell.h"
#import "DCReceiveOrderFooter.h"
#import "DCTaskDetailViewController.h"
@interface DCOrderReceiveController ()<UITableViewDelegate,UITableViewDataSource,receiveOrderFooterDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger page;


@end

@implementation DCOrderReceiveController
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackgroundColor;
        _tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTableView)];
        _tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMoeData)];
    }
    return _tableView;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getOrderReceiveData];
}
-(void)getOrderReceiveData {
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"02";
    info[@"page"] = @"1";
    info[@"limit"] = @"5";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
           
            if ([responseObject[@"count"] integerValue] != 0) {
                weakself.dataSource = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
                [weakself.view addSubview:weakself.tableView];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself setUpBgViewIsShow:NO block:nil];
            }else{
                [weakself setUpBgViewIsShow:YES block:^{
                    [weakself getOrderReceiveData];
                }];
            }

        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)refreshTableView {
    [self.tableView.mj_header endRefreshing];
    [self getOrderReceiveData];
}
-(void)reloadMoeData {
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"taskStatus"] = @"02";
    info[@"page"] = @(self.page);
    info[@"limit"] = @"5";
    __weak typeof(self) weakself = self;
    [DCServiceTool postWithUrl:getPagerTasksByCarrier_URL params:info success:^(id responseObject) {
        DCLog(@"responseObject--%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 0) {
            NSArray *array = [DCOrderReceiveModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            if (array.count != 0) {
                [weakself.dataSource addObjectsFromArray:array];
                weakself.page = [responseObject[@"page"] integerValue] +1;
                [weakself.tableView reloadData];
            }else{
               [weakself showMessage:@"已加载全部数据" afterDelay:1.0];
            }
            
        }else {
            [weakself showMessage:responseObject[@"msg"] afterDelay:1.0];
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DCReceiveOrderHeader *headerView = [[DCReceiveOrderHeader alloc]init];
    headerView.backgroundColor = BackgroundColor;
    DCOrderReceiveModel *model = self.dataSource[section];
    headerView.model = model;
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 70;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCReceiveOrderFooter *footerView = [[DCReceiveOrderFooter alloc]init];
    footerView.backgroundColor = BackgroundColor;
    DCOrderReceiveModel *model = self.dataSource[section];
    footerView.model = model;
    footerView.delegate = self;
    return footerView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DCOrderReceiveModel *model = self.dataSource[section];
    return model.oddArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCReceiveOrderCell *cell = [DCReceiveOrderCell cellWithTableView:tableView];
    DCOrderReceiveModel *orderModel = self.dataSource[indexPath.section];
    cell.model = orderModel.oddArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kBaseHeight-10)*3;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DCOrderReceiveModel *model = self.dataSource[indexPath.section];
    DCTaskDetailViewController *detailVC = [[DCTaskDetailViewController alloc]initWithOrderID:model.taskId state:@"待接单"];
    [[UIViewController currentViewController].navigationController pushViewController:detailVC animated:YES];
}

#pragma receiveOrderFooterDelegate
-(void)selectedWithCuidanBtn:(NSString *)content button:(nonnull UIButton *)button {
    DCLog(@"催单");
    __weak typeof(self) weakself = self;
    [self alertViewWithTitle:@"是否向司机发送催单信息" sure:^(BOOL isSuccess) {
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"催单信息推送成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            button.mm_acceptEventInterval = 60;//xx秒内不能再次被点击
        }];
        [alertVC addAction:cancleAction];
        [weakself presentViewController:alertVC animated:YES completion:nil];
        
    } cancle:^(bool isFaile) {
        
    }];
}
-(void)selectedWithCancleOrderBtn:(NSString *)content {
    DCLog(@"取消指令");
    __weak typeof(self) weakself = self;
    [self alertViewWithTitle:@"是否确认取消指令重新分配司机" sure:^(BOOL isSuccess) {
        DCLog(@"queren");
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"任务取消成功，请在待调度页面重新对此订单进行调度" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil];
        [alertVC addAction:cancleAction];
        [weakself presentViewController:alertVC animated:YES completion:nil];
        
    } cancle:^(bool isFaile) {
        
    }];
}

-(void)alertViewWithTitle:(NSString *)title sure:(void(^)(BOOL isSuccess))sure cancle:(void(^)(bool isFaile))cancle {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (sure) {
            sure(YES);
        }
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:sureAction];
    [alertVC addAction:cancleAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
